#include <comic_page_menu.h>
#include <string.h>
#include <comic.h>
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <debug.h>

extern Ecore_Evas *ee;
extern struct comic_reader_guis *comic;

static inline int get_index_in_tree(Evas_List *list, Etk_Tree_Row *row) {
	int i = 0;
	for (i = 0;i < evas_list_count(list);i++) {
		if ((Etk_Tree_Row *)evas_list_nth(list, i) == row) {
			return i;
		}
	}
	return -1;
}


static void __delay_remove_page(void * data) {
	struct Page_Menu *pm = (struct Page_Menu *) data;
	remove_mask(comic);
	page_menu_free(pm);
}

static Etk_Bool  jump_to_page_cb(Etk_Object *object, Etk_Tree_Row *row, Etk_Event_Mouse_Up *event, void *data) {
	struct Page_Menu *pm = (struct Page_Menu *) data;
	int index = get_index_in_tree(pm->list, row);
	if (index >= 0) {
		comic_book_goto_index(comic->book, index);
	}
	evas_object_raise(etk_embed_object_get(ETK_EMBED(pm->embed)));
	debug("jump to page \n");
	put_mask(comic);
	animator_move_object_callback(
	    etk_embed_object_get(ETK_EMBED(pm->embed)),
	    animator_square_move_cb, ANIMATOR_MAGIC,
	    0, 0 ,
	    0, -HEIGHT(),
	    &__delay_remove_page, pm);

	return ETK_TRUE;
}

struct Page_Menu * page_menu_new(struct comic_book *book) {
	int i;
	Etk_Tree_Row *row;
	char buf[64];
	if (!book) {
		return NULL;
	}
	if (evas_list_count(book->keys)<=1) {
		debug("There are no pages\n");
		return NULL;
	}
	struct Page_Menu * pm = (struct Page_Menu *) malloc (sizeof(struct Page_Menu));
	memset(pm, 0, sizeof(struct Page_Menu));
	pm->embed = etk_embed_new(ecore_evas_get(ee));
	pm->tree  = etk_tree_new();
	etk_scrolled_view_policy_set(etk_tree_scrolled_view_get(ETK_TREE(pm->tree)), 
			ETK_POLICY_HIDE, 
			ETK_POLICY_HIDE);
        etk_scrolled_view_dragable_set(
				 ETK_SCROLLED_VIEW(etk_tree_scrolled_view_get(ETK_TREE(pm->tree))),
				 ETK_TRUE);

	etk_tree_mode_set(ETK_TREE(pm->tree), ETK_TREE_MODE_LIST);
	pm->col_key = etk_tree_col_new(ETK_TREE(pm->tree), "Page" , WIDTH()*3/5, 0.0);
	//pm->col_img = etk_tree_col_new(ETK_TREE(pm->tree), "Image" , WIDTH()/5, 0.0);
	etk_tree_rows_height_set (ETK_TREE(pm->tree), 50);
	etk_tree_col_model_add(pm->col_key, etk_tree_model_text_new());
	//etk_tree_col_model_add(pm->col_img, etk_tree_model_image_new());
	etk_tree_headers_visible_set(ETK_TREE(pm->tree), 1);
	etk_tree_build(ETK_TREE(pm->tree));
	etk_container_add(ETK_CONTAINER(pm->embed), pm->tree);
	evas_object_resize(etk_embed_object_get(ETK_EMBED(pm->embed)),WIDTH(),HEIGHT());
	evas_object_move(etk_embed_object_get(ETK_EMBED(pm->embed)),0 ,-HEIGHT());
	etk_widget_show_all(pm->embed);
	for (i=0;i< evas_list_count(book->keys);i++) {
		snprintf(buf,sizeof(buf), "<b><font_size=32><center>Page: %d</></b>", i+1);
		row = etk_tree_row_append(ETK_TREE(pm->tree),NULL,
				pm->col_key,buf,
				//pm->col_img,book->name,evas_list_nth(book->keys, i),
				NULL);
		pm->list = evas_list_append(pm->list, row);
	}

	etk_signal_connect_by_code(ETK_TREE_ROW_CLICKED_SIGNAL, ETK_OBJECT(pm->tree), &jump_to_page_cb, pm);
	etk_tree_thaw(ETK_TREE(pm->tree));

	
	return pm;
}

void page_menu_free(struct Page_Menu *pm) {
	Etk_Tree_Row *row;
	if (!pm) return;
	for (;evas_list_count(pm->list)> 0;) {
		row = (Etk_Tree_Row *)evas_list_nth(pm->list, 0);
		pm->list = evas_list_remove(pm->list, row);
		etk_tree_row_delete(row);
	}
	evas_list_free(pm->list);
	etk_object_destroy(ETK_OBJECT(pm->embed));
	etk_tree_clear(ETK_TREE(pm->tree));
	etk_object_destroy(ETK_OBJECT(pm->col_key));
	//etk_object_destroy(ETK_OBJECT(pm->col_img));
	etk_object_destroy(ETK_OBJECT(pm->tree));
	free(pm);
}


